package com.pj.wisdomlogistics.mvp.model;


import com.google.gson.Gson;
import com.pj.wisdomlogistics.BuildConfig;
import com.pj.wisdomlogistics.entity.CarDetailInfo;
import com.pj.wisdomlogistics.entity.response.BaseResponse;
import com.pj.wisdomlogistics.mvp.frame.MvpModel;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import java.io.File;
import java.io.IOException;

import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;


public class CarDetailModel extends MvpModel {
    private static final String POST_URL = BuildConfig.SERVER_HOST + "carApplySign.do";

    public Observable<BaseResponse> carApplySign(
            final int carApplyID,
            final String signUID,
            final File sign,
            final int state,
            final String comment) {
        return
                Observable.create(new Observable.OnSubscribe<BaseResponse>() {
                    @Override
                    public void call(Subscriber<? super BaseResponse> subscriber) {
                        PostFormBuilder formBuilder = OkHttpUtils.post().url(POST_URL);

                        formBuilder.addParams("carApplyID", carApplyID + "");
                        formBuilder.addParams("userID", signUID);

                        if (sign != null) {
                            formBuilder.addFile("sign", sign.getAbsolutePath(), sign);
                        }

                        formBuilder.addParams("state", state + "");
                        formBuilder.addParams("comment", comment);


                        try {
                            Response response = formBuilder.build().execute();
                            String str = response.body().string();
                            BaseResponse postResponse = new Gson().fromJson(str, BaseResponse.class);
                            subscriber.onNext(postResponse);
                            subscriber.onCompleted();
                        } catch (IOException e) {
                            e.printStackTrace();
                            subscriber.onError(e);
                        }
                    }
                });
    }


    public Observable<BaseResponse<CarDetailInfo>> findCarApplyDetail(int carApplyID) {
        return mApiService.findCarApplyDetail(carApplyID);
    }

    public Observable<BaseResponse> carApplyComment(
            String userID,
            int carApplyID,
            String comment,
            int state) {
        return mApiService.carApplyComment(userID, carApplyID, comment, state);
    }


    public Observable<BaseResponse> carApplyAllot(String userID,
                                                  String carApplyID,
                                                  String state,
                                                  String driverName,
                                                  String driverTel,
                                                  String carModel,
                                                  String carNum,
                                                  String driverID,
                                                  String carID,
                                                  String comment) {
        return mApiService.carApplyAllot(
                userID,
                carApplyID,
                state,
                driverName,
                driverTel,
                carModel,
                carNum,
                driverID,
                carID,
                comment);
    }

    public Observable<BaseResponse> carApplyAssess(
            String assess ,
            String carApplyID ,
            int levels
    ) {
        return mApiService.carApplyAssess(
                assess ,
                carApplyID ,
                levels);
    }

    public Observable<BaseResponse> carApplyFinish(
            String userID ,
            int carApplyID ,
            String beforeMile ,
            String afterMile
    ){
        return mApiService.carApplyFinish(userID , carApplyID , beforeMile , afterMile);
    }

    public Observable<BaseResponse> cancelCar(
            int carApplyID
    ) {
        return mApiService.cancelCar(carApplyID);
    }
}
